﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.Content;
using System.Text;
using Cn.Cellula.Biz;
using Cn.Cellula.Client;
using System.Text.RegularExpressions;
using Cn.Cellula.Model.System;
using Cn.Edwon.Web;

namespace Cn.Cellula.Website.Admin.Item {
    public partial class ItemView : PageSupport {
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        String ItemKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            ItemKey = Request.QueryString["id"];
            LoadItemBaseInfo();
            LoadItemExtInfo();
            LoadSubItem();
        }

        private void LoadFiles(String rItemKey) {
            String hql = " from VUploadFileEO eo where eo.ModelKey='" + rItemKey + "' ";
            IList<VUploadFileEO> list = biz.Find<VUploadFileEO>(hql);
            this.gvFile.DataSource = list;
            this.gvFile.DataBind();
        }
        private void LoadItemBaseInfo() {
            VItemDetailsEO eo = biz.LoadItemDetails(ItemKey, this.Actor.OwnerKey, this.Power);
            if (eo != null) {
                this.hl_Category.Text = CategoryUtil.BuildCategoryPath(this.OwnerKey,null,eo.CategoryKey, "<a href=\"Admin/Item/List.aspx?cid={0}\">{1}</a>","&nbsp;/&nbsp;");
                this.hl_Category.NavigateUrl = "Admin/Item/List.aspx?cid=" + eo.CategoryKey;
                
                this.btn_Desc.Visible
                    = this.btn_Add.Visible
                    = this.btn_Edit.Visible
                    = this.btn_EditAtl.Visible
                    = this.btn_Files.Visible
                    = (eo.EditableRoleLevel==null 
                    || eo.EditableRoleKey==this.Power.RoleKey 
                    || eo.EditableRoleLevel < this.Power.RoleLevel);
                String desc = (String.IsNullOrEmpty(eo.ItemDesc))?"<div class=\"iva_data_desc_empty\">["+TxtDef.Item.NoData+"]</div>":eo.ItemDesc;
                this.lab_Desc.Text = desc;
                if (desc.Length > 1000) {
                    this.lab_Desc.CssClass = "desc_box_hidden";
                    this.lab_DescExpend.Text = "内容过多(" + (desc.Length/1000.0).ToString("F1") + "KB)，请点击展开：<span onclick=\"ShowItemDesc(this,event);\">&#9660;</span>";//展开
                } else {
                    this.lab_Desc.CssClass = "desc_box_display";
                    this.lab_DescExpend.Text = "<span onclick=\"ShowItemDesc(this,event);\">&#9650;</span>";//收起
                }
                this.lab_ItemName.Text = eo.ItemName;
                this.lab_ItemNo.Text = eo.ItemNo;
                this.lab_UpdateDt.Text = eo.UpdateDt.ToString();
                this.lab_ItemTags.Text = eo.ItemTags;
                this.hid_CategoryKey.Value = eo.CategoryKey;
                this.hid_ItemKey.Value = eo.ItemKey;
                LoadFiles(eo.ItemKey);
            } else {
                this.lab_ItemName.Text = "未找到属性信息";
            }
        }
        /// <summary>
        /// 装载扩展信息
        /// </summary>
        private void LoadItemExtInfo() {
            IList<VItemAtlDataEO> list = biz.LoadAtlDataByItem(ItemKey, this.OwnerKey, this.Power);
            IList<VSysAtlEO> atlList = biz.LoadSysAtlByCategory(this.hid_CategoryKey.Value,this.Power);
            StringBuilder sb = new StringBuilder();
            if (atlList != null && atlList.Count > 0) {
                sb.Append("<table cellspacing=\"0\" cellpadding=\"0\" class=\"item_view_atl\">");
                foreach (VSysAtlEO atl in atlList) {
                    sb.Append("<tr>");
                    sb.Append("<td class=\"iva_body_title\">" + atl.AtlName + "：</td>");
                    sb.Append("<td class=\"iva_body_data\">");
                    Boolean found = false;
                    String dataVal = "";
                    foreach (VItemAtlDataEO eo in list) {
                        dataVal = (String.IsNullOrEmpty(eo.RefItemName)) ? eo.DataValue : eo.RefItemName;
                        if (eo.AtlKey == atl.AtlKey) {
                            if (eo.FieldType == CodeTypeDef.FormField.Item) {
                                sb.Append("<a href=\"Admin/Item/ItemView.aspx?id=" + eo.RefItemKey + "\" >");
                                sb.Append(dataVal);
                                sb.Append("</a>");
                            } else if (eo.FieldType == CodeTypeDef.FormField.Boolean) {
                                String val = (dataVal == "0") ? TxtDef.FALSE : TxtDef.TRUE;
                                sb.Append(val);
                            } else {
                                String dval = dataVal;
                                if (dataVal != null && dataVal.Length > 18) {
                                    dval = dataVal.Substring(0, 15) + "...";
                                }
                                Regex re = new Regex("^([a-z]*)(://).*$");
                                if (re.IsMatch(eo.DataValue)) {
                                    sb.Append("<a target=\"_blank\" href=\"" + dataVal + "\">" + dval + "</a>");
                                } else {
                                    sb.Append("<font title=\"" + dataVal + "\">" + dval + "</font>");
                                }
                            }
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        if (atl.FieldType == CodeTypeDef.FormField.Item) {
                            sb.Append("<span>[<a href=\"Admin/Item/List.aspx?cid=" + atl.RefKey + "\">" + TxtDef.Item.NoData + "</a>]</span>");
                        } else {
                            sb.Append("<font>[" + TxtDef.Item.NoData + "]</font>");
                        }
                    }
                    sb.Append("</td>");
                    sb.Append("</tr>");
                }
                sb.Append("</table>");
            }
            this.lab_ExtInfo.Text = sb.ToString();
        }
        private void LoadSubItem() {
            String hql = "from VItemAtlDataEO eo where eo.OwnerKey='" + this.OwnerKey + "' ";
            hql += " and (eo.ItemVisiableRoleLevel is null or eo.ItemVisiableRoleKey='"+this.Power.RoleKey+"' or eo.ItemVisiableRoleLevel<" + this.Power.RoleLevel + ") ";
            hql += " and eo.RefItemKey='" + ItemKey + "' ";
            hql += " order by eo.ItemKey,eo.AtlOrderNumber desc ";
            IList<VItemAtlDataEO> list = biz.Find<VItemAtlDataEO>(hql);
            StringBuilder sb = new StringBuilder();
            if (list.Count > 0) {
                sb.Append("<table>");
                foreach (VItemAtlDataEO eo in list) {
                    sb.Append("<tr>");
                    sb.Append("<td>" + eo.CategoryName + "：</td>");
                    sb.Append("<td><a href=\"Admin/Item/ItemView.aspx?id=" + eo.ItemKey + "\">" + eo.ItemName + "</a>");
                    //sb.Append("<a href=\"Admin/Item/EditItem.aspx?cid="+eo.CategoryKey+"\">[+]</a>");
                    sb.Append("</td>");
                    sb.Append("</tr>");
                }
                sb.Append("</table>");
            } else {
                sb.Append("<div class=\"iva_data_desc_empty\">" + TxtDef.NoDataFound + "</div>");
            }
            this.lab_RefInfo.Text = sb.ToString();
        }

        protected void btn_Edit_Click(object sender, EventArgs e) {
            Response.Redirect("EditItem.aspx?id=" + this.hid_ItemKey.Value);
        }
        protected void btn_Desc_Click(object sender, EventArgs e) {
            Response.Redirect("EditItem.aspx?id=" + this.hid_ItemKey.Value);
        }

        protected void btn_Back_Click(object sender, EventArgs e) {
            Response.Redirect("List.aspx?cid=" + this.hid_CategoryKey.Value);
        }
        protected void btn_EditAtl_Click(object sender, EventArgs e) {
            Response.Redirect("EditAtl.aspx?id=" + this.hid_ItemKey.Value);
        }

        protected void btn_Add_Click(object sender, EventArgs e) {
            Response.Redirect("EditItem.aspx?cid=" + this.hid_CategoryKey.Value);
        }


        
    }
}